package com.put.mybills.adapter;

import java.util.List;

import com.put.mybills.R;
import com.put.mybills.model.Bill;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class BillListAdapter extends ArrayAdapter<String> {

	
	private final Context context;
	private final String[] values;
	private List<Bill> bills;
	
	private String paid;
	private String notpaid;
	
	public BillListAdapter(Context context, String[] values, List<Bill> bills, String paid , String notpaid) {
		super(context, R.drawable.bills_list_item, values);
		this.context = context;
		this.values = values;
		this.bills = bills;
		this.paid = paid;
		this.notpaid = notpaid;
		
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
		View rowView = inflater.inflate(R.drawable.bills_list_item, parent, false);
		TextView textView = (TextView) rowView.findViewById(R.id.secondLine);
		TextView textView1 = (TextView) rowView.findViewById(R.id.firstLine);
		final TextView textView3 = (TextView) rowView.findViewById(R.id.paid_tittle);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
		CheckBox chexbox =  (CheckBox)rowView.findViewById(R.id.ifBillPaid);

		final Bill bill1 = GlobalParams.getInstance().billdatasource.getBill(bills.get(position).getName());
		chexbox.setChecked(bill1.getIfPaid() == 0 ? false : true );	
		chexbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Bill bill = GlobalParams.getInstance().billdatasource.getBill(bills.get(position).getName());	
				if (isChecked == true)
				{
					bill.setIfPaid(1);
					textView3.setText(paid);
				}
				else 
				{
					bill.setIfPaid(0);	
					textView3.setText(notpaid);					
				}				
				GlobalParams.getInstance().billdatasource.updateBill(bill);
				
			}
		});
		
		textView1.setText(values[position]);
		textView.setText(bills.get(position).getDescription());			
		textView3.setText(bill1.getIfPaid() == 0 ? notpaid : paid);
		imageView.setImageResource(R.drawable.ic_cloud); 
		return rowView;
	}
	
	
	
}
